package com.partyjoin.project.module.politicalExamine.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.partyjoin.common.constant.Constants;
import com.partyjoin.common.utils.DateUtils;
import com.partyjoin.common.utils.security.ShiroUtils;
import com.partyjoin.framework.aspectj.lang.annotation.Log;
import com.partyjoin.framework.aspectj.lang.constant.BusinessType;
import com.partyjoin.framework.web.controller.BaseController;
import com.partyjoin.framework.web.domain.AjaxResult;
import com.partyjoin.framework.web.page.TableDataInfo;
import com.partyjoin.project.module.politicalExamine.domain.PoliticalExamine;
import com.partyjoin.project.module.politicalExamine.service.IPoliticalExamineService;
import com.partyjoin.project.module.proposer.domain.Proposer;
import com.partyjoin.project.module.proposer.service.IProposerService;
import com.partyjoin.project.system.user.domain.User;

/**
 * 政审记录 信息操作处理
 * 
 * @author 莫东林
 * @date 2018-07-19
 */
@Controller
@RequestMapping("/module/politicalExamine")
public class PoliticalExamineController extends BaseController
{
    private String prefix = "module/politicalExamine";
	
	@Autowired
	private IPoliticalExamineService politicalExamineService;
	
	@Autowired
	private IProposerService proposerService;
	
	@RequiresPermissions("module:politicalExamine:view")
	@GetMapping()
	public String politicalExamine()
	{
	    return prefix + "/politicalExamine";
	}
	
	/**
	 * 查询政审记录列表
	 */
	@RequiresPermissions("module:politicalExamine:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PoliticalExamine politicalExamine)
	{
		startPage();
        List<PoliticalExamine> list = politicalExamineService.selectPoliticalExamineList(politicalExamine);
		return getDataTable(list);
	}
	
	/**
	 * 保存发展对象政审函调材料
	 */
	@RequiresPermissions("module:politicalExamine:edit")
	@PostMapping("/material/save")
	@ResponseBody
	public AjaxResult materialSave(MultipartFile file, Model model, User user) throws IllegalStateException, IOException{
        User users = ShiroUtils.getUser();
        boolean success = upload(user, file, Constants.MATERIAL);
        if(success){
            PoliticalExamine p = politicalExamineService.selectPoliticalExamineByUserId(user.getUserId());
            p.setMaterial("/"+user.getUserId()+"/"+file.getOriginalFilename());
            p.setUpdateBy(users.getUserName());
            p.setUpdateTime(DateUtils.getNowDate());
            politicalExamineService.updatePoliticalExamine(p);
            return success("上传成功");
        }
		return error("上传失败");
	}
	
	/**
	 * 保存发展对象政审回函
	 */
	@RequiresPermissions("module:politicalExamine:edit")
	@PostMapping("/materialReturn/save")
	@ResponseBody
	public AjaxResult materialReturnSave(MultipartFile file, Model model, User user) throws IllegalStateException, IOException{
        User users = ShiroUtils.getUser();
        boolean success = upload(user, file, Constants.MATERIAL_RETURN);
        if(success){
            PoliticalExamine p = politicalExamineService.selectPoliticalExamineByUserId(user.getUserId());
            p.setMaterialReturn("/"+user.getUserId()+"/"+file.getOriginalFilename());
            p.setUpdateBy(users.getUserName());
            p.setUpdateTime(DateUtils.getNowDate());
            politicalExamineService.updatePoliticalExamine(p);
            return success("上传成功");
        }
		return error("上传失败");
	}
	
	/**
	 * 保存发展对象政审介绍信
	 */
	@RequiresPermissions("module:politicalExamine:edit")
	@PostMapping("/introduceLetter/save")
	@ResponseBody
	public AjaxResult introduceLetterSave(MultipartFile file, Model model, User user) throws IllegalStateException, IOException{
        User users = ShiroUtils.getUser();
        boolean success = upload(user, file, Constants.INTRODUCE_LETTER);
        if(success){
            PoliticalExamine p = politicalExamineService.selectPoliticalExamineByUserId(user.getUserId());
            p.setIntroduceLetter("/"+user.getUserId()+"/"+file.getOriginalFilename());
            p.setUpdateBy(users.getUserName());
            p.setUpdateTime(DateUtils.getNowDate());
            politicalExamineService.updatePoliticalExamine(p);
            return success("上传成功");
        }
		return error("上传失败");
	}
	
	/**
	 * 保存发展对象政审笔录
	 */
	@RequiresPermissions("module:politicalExamine:edit")
	@PostMapping("/writing/save")
	@ResponseBody
	public AjaxResult writingSave(MultipartFile file, Model model, User user) throws IllegalStateException, IOException{
        User users = ShiroUtils.getUser();
        boolean success = upload(user, file, Constants.WRITING);
        if(success){
            PoliticalExamine p = politicalExamineService.selectPoliticalExamineByUserId(user.getUserId());
            p.setWriting("/"+user.getUserId()+"/"+file.getOriginalFilename());
            p.setUpdateBy(users.getUserName());
            p.setUpdateTime(DateUtils.getNowDate());
            politicalExamineService.updatePoliticalExamine(p);
            return success("上传成功");
        }
		return error("上传失败");
	}
	
	/**
	 * 保存发展对象政审情况登记表
	 */
	@RequiresPermissions("module:politicalExamine:edit")
	@PostMapping("/verdictMaterial/save")
	@ResponseBody
	public AjaxResult verdictMaterialSave(MultipartFile file, Model model, User user) throws IllegalStateException, IOException{
        User users = ShiroUtils.getUser();
        boolean success = upload(user, file, Constants.VERDICT_MATERIAL);
        if(success){
            PoliticalExamine p = politicalExamineService.selectPoliticalExamineByUserId(user.getUserId());
            p.setVerdictMaterial("/"+user.getUserId()+"/"+file.getOriginalFilename());
            p.setUpdateBy(users.getUserName());
            p.setUpdateTime(DateUtils.getNowDate());
            politicalExamineService.updatePoliticalExamine(p);
            return success("上传成功");
        }
		return error("上传失败");
	}
	
	/**
	 * 新增政审记录
	 */
	@RequiresPermissions("module:politicalExamine:add")
	@Log(title = "政审记录", action = BusinessType.INSERT)
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 修改政审记录
	 */
	@RequiresPermissions("module:politicalExamine:edit")
	@Log(title = "政审记录", action = BusinessType.UPDATE)
	@GetMapping("/edit/{examineId}")
	public String edit(@PathVariable("examineId") Integer examineId, Model model)
	{
		PoliticalExamine politicalExamine = politicalExamineService.selectPoliticalExamineById(examineId);
		model.addAttribute("politicalExamine", politicalExamine);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存政审记录
	 */
	@RequiresPermissions("module:politicalExamine:save")
	@Log(title = "政审记录", action = BusinessType.SAVE)
	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(PoliticalExamine politicalExamine)
	{
		if (politicalExamineService.savePoliticalExamine(politicalExamine) > 0)
		{
			return success();
		}
		return error();
	}
	
	/**
	 * 删除政审记录
	 */
	@RequiresPermissions("module:politicalExamine:remove")
	@Log(title = "政审记录", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		int rows = politicalExamineService.deletePoliticalExamineByIds(ids);
		if (rows > 0)
        {
            return success();
        }
        return error();
	}
	
	/**
	 * 文件上传
	 */
	public boolean upload(User user, MultipartFile file, String type){
		boolean success = false;
		try {
			File document = null;
			String fileName = file.getOriginalFilename();  
			File firstFolder = new File(type);
			File secondFolder = new File(firstFolder + "/" + user.getUserId());
			if(!firstFolder.exists()){
				firstFolder.mkdirs();
			}
			if(!secondFolder.exists()){
				secondFolder.mkdirs();
			}
			document = new File(secondFolder+"/",fileName);
			System.out.println("文件上传路径"+ document);
	        file.transferTo(document); 
	        success = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return success;
	}
	
}
